# Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
# All rights reserved.
#
# This software is supplied "AS IS" without any warranties.
# RDA assumes no responsibility or liability for the use of the software,
# conveys no license or title under any patent, copyright, or mask work
# right to the product. RDA reserves the right to make changes in the
# software without notification.  RDA also make no representation or
# warranty that such application will be suitable for the specified use
# without further testing or modification.

cmake_minimum_required(VERSION 3.13)

set(SOURCE_TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(BINARY_TOP_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(CMAKE_C_COMPILER_FORCED   1)
set(CMAKE_CXX_COMPILER_FORCED 1)

set(out_hex_dir ${CMAKE_CURRENT_BINARY_DIR}/hex)
set(out_lib_dir ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(out_inc_dir ${CMAKE_CURRENT_BINARY_DIR}/include)
set(tools_dir ${CMAKE_CURRENT_SOURCE_DIR}/tools)

set(core_stub_o ${CMAKE_CURRENT_SOURCE_DIR}/components/lib/core_stub.o)
set(flash_ldscript ${SOURCE_TOP_DIR}/ldscripts/app_flashimg.ld)
set(file_ldscript ${SOURCE_TOP_DIR}/ldscripts/app_fileimg.ld)
set(cmd_mkappimg dtools mkappimg)
set(pacgen_py ${tools_dir}/pacgen.py)

include(cmake/core_config.cmake)
include(cmake/toolchain-gcc.cmake)
set(BUILD_SHARED_LIBS OFF)

project(core C CXX ASM)

include_directories(components/include
components/newlib/include 
source/basic_service/net_manage
source/basic_service/log_manage
source/basic_service/mon_manage
source/basic_service/gnss_manage
source/basic_service/rtc_manage
source/basic_service/power_manage
source/basic_service/event_manage
source/basic_service/port_manage
source/basic_service/mcu_manage
source/basic_service/usb_manage
source/basic_service/qxwz_manage
source/basic_service/save_manage
source/basic_service/ble_manage
source/basic_service/battery_manage
source/basic_service/gsen_manage
source/basic_service/lbs_manage

source/app_service/vehicle_process
source/app_service/national_32960
source/app_service/unit_testing
source/app_service/national_808
source/app_service/bluetooth_control

source/config_lib/can_cfg
source/config_lib/com_lib
source/config_lib/flash_cfg
source/config_lib/err_list

source/gaoz_api
source/os_app)

function(cpp_only target file)
    add_library(${target} OBJECT ${file})
    set_source_files_properties(${file} PROPERTIES LANGUAGE C)
    target_compile_options(${target} PRIVATE -E -P -x c)
endfunction()

function(add_appimg target ldscript)
    set(gen_ldscript ${target}_ldscript)
    set(target_map_file ${out_hex_dir}/${target}.map)
    set(target_img_file ${out_hex_dir}/${target}.img)
    cpp_only(${gen_ldscript} ${ldscript})
    add_executable(${target} ${ARGN} ${core_stub_o})
    set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${out_hex_dir})
    target_link_libraries(${target} PRIVATE -T $<TARGET_OBJECTS:${gen_ldscript}>)
    target_link_libraries(${target} PRIVATE -Wl,-Map=${target_map_file} -nostdlib -Wl,--gc-sections)

    add_custom_command(TARGET ${target} POST_BUILD
        COMMAND ${cmd_mkappimg} $<TARGET_FILE:${target}> ${target_img_file}
        BYPRODUCTS ${target_img_file} ${target_map_file}
    )
endfunction()

configure_file(${SOURCE_TOP_DIR}/components/lib/fdl1.img ${out_hex_dir}/fdl1.img COPYONLY)
configure_file(${SOURCE_TOP_DIR}/components/lib/fdl2.img ${out_hex_dir}/fdl2.img COPYONLY)
macro(pac_init_fdl cmd)
    set(${cmd}
        cfg-init --pname UIX8910_MODEM --palias "APPIMG"
            --pversion "8910 MODULE" --version "BP_R1.0.0"
            --flashtype 1
        cfg-host-fdl -a ${CONFIG_FDL1_IMAGE_START} -s ${CONFIG_FDL1_IMAGE_SIZE}
            -p ${out_hex_dir}/fdl1.img
        cfg-fdl2 -a ${CONFIG_FDL2_IMAGE_START} -s ${CONFIG_FDL2_IMAGE_SIZE}
            -p ${out_hex_dir}/fdl2.img
    )
endmacro()

if(CONFIG_APPIMG_LOAD_FLASH)
    set(target appimg_flash_delete)
    set(pac_file ${out_hex_dir}/${target}.pac)
    pac_init_fdl(init_fdl)
    add_custom_command(OUTPUT ${pac_file}
        COMMAND python3 ${pacgen_py} ${init_fdl}
            cfg-erase-flash -i ERASE_APPIMG
                -a ${CONFIG_APPIMG_FLASH_ADDRESS}
                -s ${CONFIG_APPIMG_FLASH_SIZE}
            pac-gen ${pac_file}
        DEPENDS ${pacgen_py}
        WORKING_DIRECTORY ${SOURCE_TOP_DIR}
    )
    add_custom_target(${target}_pacgen ALL DEPENDS ${pac_file})
endif()

if(CONFIG_APPIMG_LOAD_FILE)
    set(target appimg_file_delete)
    set(pac_file ${out_hex_dir}/${target}.pac)
    pac_init_fdl(init_fdl)
    add_custom_command(OUTPUT ${pac_file}
        COMMAND python3 ${pacgen_py} ${init_fdl}
            cfg-del-appimg -i DEL_APPIMG
            pac-gen ${pac_file}
        DEPENDS ${pacgen_py}
        WORKING_DIRECTORY ${SOURCE_TOP_DIR}
    )
    add_custom_target(${target}_pacgen ALL DEPENDS ${pac_file})
endif()

file(GLOB LIB_SOURCES_FILE ${CMAKE_SOURCE_DIR}/source/app_service/*.c)
file(GLOB LIB_SOURCESBASIC_NET_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/net_manage/*.c)
file(GLOB LIB_SOURCESBASIC_LOG_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/log_manage/*.c)
file(GLOB LIB_SOURCESBASIC_MON_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/mon_manage/*.c)
file(GLOB LIB_SOURCESBASIC_GPS_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/gnss_manage/*.c)
file(GLOB LIB_SOURCESBASIC_RTC_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/rtc_manage/*.c)
file(GLOB LIB_SOURCESBASIC_PWR_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/power_manage/*.c)
file(GLOB LIB_SOURCESBASIC_ENT_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/event_manage/*.c)
file(GLOB LIB_SOURCESBASIC_POR_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/port_manage/*.c)
file(GLOB LIB_SOURCESBASIC_MCU_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/mcu_manage/*.c)
file(GLOB LIB_SOURCESBASIC_USB_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/usb_manage/*.c)
file(GLOB LIB_SOURCESBASIC_QXWZ_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/qxwz_manage/*.c)
file(GLOB LIB_SOURCESBASIC_SAVE_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/save_manage/*.c)
file(GLOB LIB_SOURCESBASIC_BLE_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/ble_manage/*.c)
file(GLOB LIB_SOURCESBASIC_BAT_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/battery_manage/*.c)
file(GLOB LIB_SOURCESBASIC_GSEN_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/gsen_manage/*.c)
file(GLOB LIB_SOURCESBASIC_LBS_FILE ${CMAKE_SOURCE_DIR}/source/basic_service/lbs_manage/*.c)

file(GLOB LIB_SOURCESBASIC_VEH_FILE ${CMAKE_SOURCE_DIR}/source/app_service/vehicle_process/*.c)
file(GLOB LIB_SOURCESBASIC_NAT_FILE ${CMAKE_SOURCE_DIR}/source/app_service/national_32960/*.c)
file(GLOB LIB_SOURCESBASIC_UNT_FILE ${CMAKE_SOURCE_DIR}/source/app_service/unit_testing/*.c)
file(GLOB LIB_SOURCESBASIC_808_FILE ${CMAKE_SOURCE_DIR}/source/app_service/national_808/*.c)
file(GLOB LIB_SOURCESBASIC_BTC_FILE ${CMAKE_SOURCE_DIR}/source/app_service/bluetooth_control/*.c)


file(GLOB LIB_SOURCESBASIC_CAN_LIB_FILE ${CMAKE_SOURCE_DIR}/source/config_lib/can_cfg/*.c)
file(GLOB LIB_SOURCESBASIC_COM_LIB_FILE ${CMAKE_SOURCE_DIR}/source/config_lib/com_lib/*.c)
file(GLOB LIB_SOURCESBASIC_FLS_LIB_FILE ${CMAKE_SOURCE_DIR}/source/config_lib/flash_cfg/*.c)
file(GLOB LIB_SOURCESBASIC_ERR_LIB_FILE ${CMAKE_SOURCE_DIR}/source/config_lib/err_list/*.c)

file(GLOB LIB_SOURCESOS_GAOZ_API_FILE ${CMAKE_SOURCE_DIR}/source/gaoz_api/*.c)
file(GLOB LIB_SOURCESOS_OS_APP_FILE ${CMAKE_SOURCE_DIR}/source/os_app/*.c)
file(GLOB LIB_STARTAPP_FILE ${CMAKE_SOURCE_DIR}/source/*.c)

if(CONFIG_APPIMG_LOAD_FLASH)
    set(target GZ801B34GLGT00)
    add_appimg(${target} ${flash_ldscript} 
    ${LIB_SOURCES_FILE}
    ${LIB_SOURCESBASIC_NET_FILE}
    ${LIB_SOURCESBASIC_LOG_FILE}
    ${LIB_SOURCESBASIC_MON_FILE}
    ${LIB_SOURCESBASIC_GPS_FILE}
    ${LIB_SOURCESBASIC_RTC_FILE}
    ${LIB_SOURCESBASIC_PWR_FILE}
    ${LIB_SOURCESBASIC_ENT_FILE}
    ${LIB_SOURCESBASIC_POR_FILE}
    ${LIB_SOURCESBASIC_MCU_FILE}
	${LIB_SOURCESBASIC_USB_FILE}
    ${LIB_SOURCESBASIC_QXWZ_FILE}
    ${LIB_SOURCESBASIC_SAVE_FILE}
	${LIB_SOURCESBASIC_BLE_FILE}
	${LIB_SOURCESBASIC_BAT_FILE}
	${LIB_SOURCESBASIC_GSEN_FILE}
    ${LIB_SOURCESBASIC_LBS_FILE}

	${LIB_SOURCESBASIC_VEH_FILE}
    ${LIB_SOURCESBASIC_NAT_FILE}
    ${LIB_SOURCESBASIC_UNT_FILE}
    ${LIB_SOURCESBASIC_808_FILE}
    ${LIB_SOURCESBASIC_BTC_FILE}
    
	${LIB_SOURCESBASIC_CAN_LIB_FILE}
    ${LIB_SOURCESBASIC_COM_LIB_FILE} 
    ${LIB_SOURCESBASIC_FLS_LIB_FILE}
    ${LIB_SOURCESBASIC_ERR_LIB_FILE}
    ${LIB_SOURCESOS_GAOZ_API_FILE}
    ${LIB_SOURCESOS_OS_APP_FILE}
    ${LIB_STARTAPP_FILE})
    target_link_libraries(${target} PRIVATE ${libc_file_name} ${libm_file_name} ${libgcc_file_name})
    target_link_libraries(${target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/source/basic_service/qxwz_manage/libqxsdk.a)
    target_link_libraries(${target} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/source/basic_service/qxwz_manage/libqxrtk_engine.a)
    target_link_libraries(${target} PRIVATE ${libgcc_file_name})
	target_link_libraries(${target} PRIVATE ${libm_file_name})
	target_link_libraries(${target} PRIVATE ${libc_file_name})
	target_compile_definitions(${target} PRIVATE APP_VERSION="101.100.100")
    set(pac_file ${out_hex_dir}/${target}.pac)
    set(target_img_file ${out_hex_dir}/${target}.img)
    pac_init_fdl(init_fdl)
    add_custom_command(OUTPUT ${pac_file}
        COMMAND python3 ${pacgen_py} ${init_fdl}
            cfg-image -i APPIMG -a ${CONFIG_APPIMG_FLASH_ADDRESS}
                -s ${CONFIG_APPIMG_FLASH_SIZE}
                -p ${target_img_file}
            pac-gen ${pac_file}
        DEPENDS ${pacgen_py} ${target_img_file}
        WORKING_DIRECTORY ${SOURCE_TOP_DIR}
    )
    add_custom_target(${target}_pacgen ALL DEPENDS ${pac_file})
endif()

if(CONFIG_APPIMG_LOAD_FILE)
    set(target hello_file)
    add_appimg(${target} ${file_ldscript} hello_world.c)
    target_link_libraries(${target} PRIVATE ${libc_file_name} ${libm_file_name} ${libgcc_file_name})

    set(pac_file ${out_hex_dir}/${target}.pac)
    set(target_img_file ${out_hex_dir}/${target}.img)
    pac_init_fdl(init_fdl)
    add_custom_command(OUTPUT ${pac_file}
        COMMAND python3 ${pacgen_py} ${init_fdl}
            cfg-pack-file -i APPIMG -p ${target_img_file}
                -n ${CONFIG_APPIMG_LOAD_FILE_NAME}
            pac-gen ${pac_file}
        DEPENDS ${pacgen_py} ${target_img_file}
        WORKING_DIRECTORY ${SOURCE_TOP_DIR}
    )
    add_custom_target(${target}_pacgen ALL DEPENDS ${pac_file})
endif()
